package office.action.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import office.action.system.vo.DepartmentVo;
import office.bo.system.Department;
import office.service.system.ISystemService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import platform.common.action.BaseAction;

@Namespace("/system")
@ParentPackage("struts-base")
@Controller
@Scope("prototype")
public class SystemAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<DepartmentVo> departmentList = new ArrayList<DepartmentVo>();
	private ISystemService systemService;
	private DepartmentVo departmentVo;
	
	@Action(value="saveDepartment")
	public String saveDepartment() throws Exception{
		try{
		Department department = new Department();
		department.setId(departmentVo.getId());
		department.setIsolatedFlag("department");
		department.setName(departmentVo.getName());
		department.setMemo(departmentVo.getMemo());
		department.setStrutSign("department");
		department.setCreateDate(departmentVo.getCreateDate());
		department.setType(departmentVo.getType());
		systemService.saveDepartment(departmentVo.getParentId(), department);
		this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		}catch(Exception e){
			e.printStackTrace();
		}
		return RESULT_AJAXJSON;
	}
	
	
	@Action(value="gotoDepartment",results={@Result(location="/pages/system/departmentEdit.jsp")})
	public String gotoLogMenu(){
		return "success";
	}
	
	@Action(value="gotoDepartmentMenu",results={@Result(location="/pages/system/departmentXml.jsp")})
	public String gotoDepartmentMenu(){
		return "success";
	}
	
	@Action(value="getDepartmentById",results={@Result(location="/pages/system/departmentEdit.jsp")})
	public String getDepartmentById(){
		Department department = systemService.getDepartmentById(departmentVo.getId());
		if(department != null){
			departmentVo = new DepartmentVo();
			departmentVo.setId(department.getId().toString());
			departmentVo.setName(department.getName());
			departmentVo.setMemo(department.getMemo());
			departmentVo.setType(department.getType());
			departmentVo.setCompanyId(department.getCompanyId());
			departmentVo.setCompanyName(departmentVo.getCompanyName());
		}
		return "success";
	}
	
	@Action(value="gotoDeleteDepartment",results={@Result(location="/pages/system/departmentDel.jsp")})
	public String gotoDeleteDepartment(){
		return "success";
	}
	
	@Action(value="getAllDepartment",results={@Result(location="/pages/system/department.jsp")})
	public String getAllDepartment(){
		
		List<Department> list = systemService.getAllDepartment();
		for (Iterator<Department> iterator = list.iterator(); iterator.hasNext();) {
			Department department = (Department) iterator.next();
			DepartmentVo departmentVo = new DepartmentVo();
			departmentVo.setId(department.getId().toString());
			departmentVo.setName(department.getName());
			departmentVo.setMemo(department.getMemo());
			departmentVo.setCreateDate(department.getCreateDate());
			departmentVo.setType(department.getType());
			departmentVo.setCompanyId(department.getCompanyId());
			departmentVo.setCompanyName(departmentVo.getCompanyName());
			departmentVo.setLeftValue(department.getLeftValue());
			departmentVo.setRightValue(department.getRightValue());
			departmentVo.setTreeLevel(department.getTreeLevel());
			departmentList.add(departmentVo);
		}
		return "success";
	}
	@Action(value="deleteDepartmentById")
	public String deleteDepartmentById(){
		systemService.deleteDepartmentById(departmentVo.getId());
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}

	public List<DepartmentVo> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<DepartmentVo> departmentList) {
		this.departmentList = departmentList;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public DepartmentVo getDepartmentVo() {
		return departmentVo;
	}

	public void setDepartmentVo(DepartmentVo departmentVo) {
		this.departmentVo = departmentVo;
	}

}
